package app1.rapidsquare.com.fragment;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import app1.rapidsquare.com.activity.HowItWorksActivity;

public class HowItWorksFragment extends Fragment {

    private int mImageResourceId;
    private static final String KEY_RESOURCE = "HowItWorksFragment:ImageResource";
    private ImageView mImageView;

    public static HowItWorksFragment newInstance(int imageResource) {
        HowItWorksFragment howItWorksFragment = new HowItWorksFragment();

        howItWorksFragment.mImageResourceId = imageResource;
        return howItWorksFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mImageView = new ImageView(getActivity());
        mImageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mImageView.setBackgroundColor(getResources().getColor(android.R.color.white));

        LinearLayout layout = new LinearLayout(getActivity());
        layout.setBackgroundColor(getResources().getColor(android.R.color.white));
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        layout.setGravity(Gravity.CENTER);
        layout.addView(mImageView);

        return layout;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (savedInstanceState != null && savedInstanceState.containsKey(KEY_RESOURCE)) {
            mImageResourceId = savedInstanceState.getInt(KEY_RESOURCE);
        }

        if (HowItWorksActivity.class.isInstance(getActivity())) {
            ((HowItWorksActivity) getActivity()).LoadBitmap(mImageResourceId, mImageView);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_RESOURCE, mImageResourceId);
    }


}
